/**
 * IK 中文分词  版本 5.0
 * IK Analyzer release 5.0
 * <p>
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * <p>
 * 源代码由林良益(linliangyi2005@gmail.com)提供
 * 版权声明 2012，乌龙茶工作室
 * provided by Linliangyi and copyright 2012 by Oolong studio
 */
package com.metis.document.parse.dialog.utils.ikseg.dic;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import com.metis.document.parse.dialog.utils.ikseg.cfg.IkConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;




/**
 * 词典管理类,单子模式
 */
@Slf4j
public class Dictionary {

    /*
     * 词典单子实例
     */
    private static Dictionary singleton;

    private DictSegment _MainDict;

    private DictSegment _QuantifierDict;

    private DictSegment _StopWords;

    /**
     * 配置对象
     */
    private IkConfiguration ikConfiguration;

    private static final String PATH_DIC_MAIN = "dict/main.dic";
    private static final String PATH_DIC_SURNAME = "dict/surname.dic";
    private static final String PATH_DIC_QUANTIFIER = "dict/quantifier.dic";
    private static final String PATH_DIC_SUFFIX = "dict/suffix.dic";
    private static final String PATH_DIC_PREP = "dict/preposition.dic";
    private static final String PATH_DIC_STOP = "dict/stopword.dic";

    private static final String FILE_NAME = "dict/IKAnalyzer.cfg.xml";
    private static final String EXT_DICT = "ext_dict";
    private static final String REMOTE_EXT_DICT = "remote_ext_dict";
    private static final String EXT_STOP = "ext_stopwords";
    private static final String REMOTE_EXT_STOP = "remote_ext_stopwords";

        private Path conf_dir;
    private Properties props;

    private Dictionary(IkConfiguration cfg) {
        this.ikConfiguration = cfg;
        this.props = new Properties();
       // this.conf_dir = Paths.get("/Users/codexvn/IdeaProjects/metis-document-parse/metis-document-parsing/src/main/resources/dict");
        this.conf_dir = Paths.get("D:\\code\\yl\\metis-document-parse-dialog\\src\\main\\resources\\dict");
    }

    private String getProperty(String key) {
        if (props != null) {
            return props.getProperty(key);
        }
        return null;
    }

    /**
     * 词典初始化 由于IK Analyzer的词典采用Dictionary类的静态方法进行词典初始化
     * 只有当Dictionary类被实际调用时，才会开始载入词典， 这将延长首次分词操作的时间 该方法提供了一个在应用加载阶段就初始化字典的手段
     *
     * @return Dictionary
     */
    public static synchronized void initial(IkConfiguration cfg) {
        if (singleton == null) {
            synchronized (Dictionary.class) {
                if (singleton == null) {

                    singleton = new Dictionary(cfg);
                    singleton.loadMainDict();
                    singleton.loadSurnameDict();
                    singleton.loadQuantifierDict();
                    singleton.loadSuffixDict();
                    singleton.loadPrepDict();
                    singleton.loadStopWordDict();
                }
            }
        }
    }

    private void walkFileTree(List<String> files, Path path) {
        if (Files.isRegularFile(path)) {
            files.add(path.toString());
        } else if (Files.isDirectory(path)) try {
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                    files.add(file.toString());
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFileFailed(Path file, IOException e) {
                    log.error("[Ext Loading] listing files", e);
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            log.error("[Ext Loading] listing files", e);
        }
        else {
            log.warn("[Ext Loading] file not found: " + path);
        }
    }

    private void loadDictFile(DictSegment dict, Resource resource, boolean critical, String name) {
        try (InputStream is = resource.getInputStream()) {
            BufferedReader br = new BufferedReader(
                new InputStreamReader(is, StandardCharsets.UTF_8), 512);
            String word = br.readLine();
            if (word != null) {
                if (word.startsWith("\uFEFF"))
                    word = word.substring(1);
                for (; word != null; word = br.readLine()) {
                    word = word.trim();
                    if (word.isEmpty()) continue;
                    dict.fillSegment(word.toCharArray());
                }
            }
        } catch (IOException e) {
            log.error("ik-analyzer: " + name + " loading failed", e);
        }
    }

    private List<String> getExtDictionarys() {
        List<String> extDictFiles = new ArrayList<String>(2);
        String extDictCfg = getProperty(EXT_DICT);
        if (extDictCfg != null) {

            String[] filePaths = extDictCfg.split(";");
            for (String filePath : filePaths) {
                if (filePath != null && !"".equals(filePath.trim())) {
                    Path file = FileSystems.getDefault().getPath(getDictRoot(), filePath.trim());
                    walkFileTree(extDictFiles, file);

                }
            }
        }
        return extDictFiles;
    }


    private List<String> getExtStopWordDictionarys() {
//        List<String> extStopWordDictFiles = new ArrayList<String>(2);
//        String extStopWordDictCfg = getProperty(EXT_STOP);
//        if (extStopWordDictCfg != null) {
//
//            String[] filePaths = extStopWordDictCfg.split(";");
//            for (String filePath : filePaths) {
//                if (filePath != null && !"".equals(filePath.trim())) {
//                    Path file = PathUtils.get(getDictRoot(), filePath.trim());
//                    walkFileTree(extStopWordDictFiles, file);
//
//                }
//            }
//        }
//        return extStopWordDictFiles;
        return Collections.emptyList();
    }

    private String getDictRoot() {
        return conf_dir.toAbsolutePath().toString();
    }


    /**
     * 获取词典单子实例
     *
     * @return Dictionary 单例对象
     */
    public static Dictionary getSingleton() {
        if (singleton == null) {
            throw new IllegalStateException("ik dict has not been initialized yet, please call initial method first.");
        }
        return singleton;
    }


    /**
     * 批量加载新词条
     *
     * @param words
     *            Collection<String>词条列表
     */
    public void addWords(Collection<String> words) {
        if (words != null) {
            for (String word : words) {
                if (word != null) {
                    // 批量加载词条到主内存词典中
                    singleton._MainDict.fillSegment(word.trim().toCharArray());
                }
            }
        }
    }

    /**
     * 批量移除（屏蔽）词条
     */
    public void disableWords(Collection<String> words) {
        if (words != null) {
            for (String word : words) {
                if (word != null) {
                    // 批量屏蔽词条
                    singleton._MainDict.disableSegment(word.trim().toCharArray());
                }
            }
        }
    }

    /**
     * 检索匹配主词典
     *
     * @return Hit 匹配结果描述
     */
    public Hit matchInMainDict(char[] charArray) {
        return singleton._MainDict.match(charArray);
    }

    /**
     * 检索匹配主词典
     *
     * @return Hit 匹配结果描述
     */
    public Hit matchInMainDict(char[] charArray, int begin, int length) {
        return singleton._MainDict.match(charArray, begin, length);
    }

    /**
     * 检索匹配量词词典
     *
     * @return Hit 匹配结果描述
     */
    public Hit matchInQuantifierDict(char[] charArray, int begin, int length) {
        return singleton._QuantifierDict.match(charArray, begin, length);
    }

    /**
     * 从已匹配的Hit中直接取出DictSegment，继续向下匹配
     *
     * @return Hit
     */
    public Hit matchWithHit(char[] charArray, int currentIndex, Hit matchedHit) {
        DictSegment ds = matchedHit.getMatchedDictSegment();
        return ds.match(charArray, currentIndex, 1, matchedHit);
    }

    /**
     * 判断是否是停止词
     *
     * @return boolean
     */
    public boolean isStopWord(char[] charArray, int begin, int length) {
        return singleton._StopWords.match(charArray, begin, length).isMatch();
    }

    /**
     * 加载主词典及扩展词典
     */
    private void loadMainDict() {
        // 建立一个主词典实例
        _MainDict = new DictSegment((char) 0);

        // 读取主词典文件
        Resource resource = new ClassPathResource(Dictionary.PATH_DIC_MAIN);
        loadDictFile(_MainDict, resource, false, "Main Dict");
    }

    /**
     * 加载用户扩展的停止词词典
     */
    private void loadStopWordDict() {
        // 建立主词典实例
        _StopWords = new DictSegment((char) 0);

        // 读取主词典文件
        Resource resource = new ClassPathResource( Dictionary.PATH_DIC_STOP);
        loadDictFile(_StopWords, resource, false, "Main Stopwords");
    }

    /**
     * 加载量词词典
     */
    private void loadQuantifierDict() {
        // 建立一个量词典实例
        _QuantifierDict = new DictSegment((char) 0);
        // 读取量词词典文件
        Resource resource = new ClassPathResource(Dictionary.PATH_DIC_QUANTIFIER);
        loadDictFile(_QuantifierDict, resource, false, "Quantifier");
    }

    private void loadSurnameDict() {
        DictSegment _SurnameDict = new DictSegment((char) 0);
        Resource resource = new ClassPathResource(Dictionary.PATH_DIC_SURNAME);
        loadDictFile(_SurnameDict, resource, true, "Surname");
    }

    private void loadSuffixDict() {
        DictSegment _SuffixDict = new DictSegment((char) 0);
        Resource resource = new ClassPathResource(Dictionary.PATH_DIC_SUFFIX);
        loadDictFile(_SuffixDict, resource, true, "Suffix");
    }

    private void loadPrepDict() {
        DictSegment _PrepDict = new DictSegment((char) 0);
        Resource resource = new ClassPathResource( Dictionary.PATH_DIC_PREP);
        loadDictFile(_PrepDict, resource, true, "Preposition");
    }
}
